import base64
import json
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex


class PrpCrypt(object):
    def __init__(self, key, iv):
        self.key = key.encode('utf-8')
        self.mode = AES.MODE_CBC
        self.iv = iv.encode('utf-8')

    def pad_byte(self, b):
        '''
        1 先计算所传入bytes类型文本与16的余数
        2 在将此余数转成bytes 当然用0补位也可以
        3 已知了 余数 那么就用余数*被转成的余数，就得到了需要补全的bytes
        4 拼接原有文本和补位
        :param b: bytes类型的文本
        :return: 返回补全后的bytes文本
        '''
        bytes_num_to_pad = AES.block_size - (len(b) % AES.block_size)
        # python3 中默认unicode转码
        # 实际上byte_to_pad 就已经 将 数字转成了unicode 对应的字符  即使你的入参正好是16的倍数，那么bytes也是把列表整体的转码也是有值的
        # 后边解密的匿名函数 拿到最后一个数字后，就知道应该截取的长度，在反着切片就行了
        # 这样保证了数据的完整性
        byte_to_pad = bytes([bytes_num_to_pad])
        padding = byte_to_pad * bytes_num_to_pad
        padded = b + padding
        return padded

    def encrypt(self, text):
        '''
        1 先生成aes实例
        2 对传入的text转成bytes
        3 对传入的text补全
        4 调用encrypt 加密 得到密文
        5 先将密文转16进制，在将16进制用base64转码，然后在将得到的base64解码
        其实在步骤4 就已经完成了aes加密，我所在的公司加密比较复杂 ，需要的可以直接返回步骤4的值

        :param text:
        :return:
        '''
        cryptor = AES.new(self.key, self.mode, self.iv)
        text = text.encode('utf-8')
        text = self.pad_byte(text)
        self.ciphertext = cryptor.encrypt(text)
        # cryptbase64 = base64.b64encode(b2a_hex(self.ciphertext)).decode('utf8')
        # return cryptbase64
        cryptbase64 = base64.b64encode(self.ciphertext).decode('utf8')
        print(cryptbase64)
        return cryptbase64

    def decrypt(self, text):
        '''
        解密和加密的顺序是相反的
        1 定义匿名函数，去掉补位
        2 base64解码
        3 生成aes实例
        # 4 16进制转2进制
        5 使用decrypt解码  得到补全的bytes类型明文

        :param text:
        :return:  解密且去掉补位的明文
        '''
        def unpad(s): return s[:-ord(s[len(s) - 1:])]
        base64Str = base64.b64decode(text.encode('utf8'))
        cryptor = AES.new(self.key, self.mode, self.iv)
        # aesStr = cryptor.decrypt(a2b_hex(base64Str))
        aesStr = cryptor.decrypt(base64Str).decode('utf8')
        aesStr = unpad(aesStr)
        print(aesStr)
        return aesStr


if __name__ == '__main__':
    print('----')
    print('-----解码-----')
    # request = {
    #     "android_id": "04d4c4248cc29967",
    #     "idCard": "140100199401010099",
    #     "imei": "863254010244902",
    #     "introduction": "0",
    #     "isVM": "1",
    #     "mac": "08:00:27:D0:5A:66",
    #     "pid": "103866",
    #     "platform": "Android",
    #     "realName": "里杰卡尔德",
    #     "signType": "RSA",
    #     "tel": "13205776136",
    #     "timestamp": "2020-12-10 10:03:27",
    #     "unit": "VOG-AL10",
    #     "username": "ld964yxtest",
    #     "version": "1.0",
    #     "sign": "NcjfLwiNwtL299phbmAiZLFkx2DS/SyZG2g7GTWullo56P2Ex4enhC3WlXoMihIIRw5T2DLgEafLT4KqlBZxDsUHQL2/paKvQqClsDU5V1ubzizPwDd1iJgM3p84e7gHYIE36hQjPh75/Z/csLR+MDDquEjZ4hfL/EDlIGUxsskuqotE7nKIrLvyUyP+CL8KU2WT9HmlGREVvMvz/Pkkak2VmyeI/RkrFnD30K0pV7n23aMlqG26xLdQWuf700c+Vn774HPeciM2kGCiK/rZh5N8FWVLlkgTWHNkMdsBxzk5p6b3siMyyPb61kTzc5F6+gtu/t5omxBS1ZlltO5qGg=="
    # }
    request = {
          "accessToken": "QXFxUnI3eHYveWNnNDVWMkhyV2F2RzRac2ZSUTg5T3EwM1BYUnFHalBvU0JETjBNN3JGM3B6N3lWSkc4K0pWQm1tZklXZVZVTkMvOHFsdngrbkpSckhWM2R0TGwvU3R3SjV0bk4wV3VWN1E9",
          "android_id": "89b55ae86c63da6b",
          "imei": "866174491984528",
          "introduction": "V7HzWaKgyKmjwTkDZk5%2F9w%3D%3D",
          "isVM": "0",
          "loginType": "4",
          "mac": "08:00:27:2D:D8:96",
          "pid": "104725",
          "platform": "Android",
          "refreshToken": "Sm1SbGJQZzF5WDUyMkhVdUV2ZXFrcDNkK3BTTHRTS2FuOVplZS9WT0xjeTQ4aHdEK2FYRWtKMmVMN0NjT1RXNDlVRHMzVnlaRFZuY0pWdnJkT1hGUTBUVGJkMW9xbVF6SkRlRHlyYktSVjQ9",
          "signType": "RSA",
          "tel": "13815335078",
          "timestamp": "2021-04-14 10:41:46",
          "unit": "OPPO R11 Plus",
          "username": "UC191812",
          "version": "1.1",
          "sign": "Mc3Dtnr/t4Ur4PdIOPzVKEPN1ciOIhn4ujfu7ePzQgXt6yMOnxM+rZcSH8Ss78E041m0o+T9CEGC\nwS6mOKAfihW5GOYDatY9hwWVrtc7MmVAOsTiJKHRESloEiEiTboMZkuuG4DRM3LCPOqDiKQp2r9v\nAkV555IuQCHIrUrEwTTHwx/1cNkzsHjNLqZoJeHAgHNVDuAe+TEdaVFpUDKYqZZKy6iJXaXLFTu/\nf4rncgHjg+mowbp5Aww0+ZrHMeoUuxHBXwfvrqNr9ikwH1qlgW/E0m/MCP8cvBVAbV6fKrgynIyT\nssSWNBfNL+58BxHR449ElxYB1rG09TYfDoUrBg\u003d\u003d\n"
        }
    pc2 = PrpCrypt('amvCpadghXNnORw74LG2QdT7pwKz9kci', '8229165556696540')
    #s_test = '''u7RkoXyz51cnTt++6AEGAIa3xMgiiKNTVI1yjJ8RG4dzB3kNTVkRJoSJFWllKIO7whOEZzI6HnWwdiNnbSZDfUSlNiuMtIKUdDX2QDian4aGrXJz5GZYVHWAmmBgw5gYEK5J/esdz61mao4SEzbaHfUZm/E2enov5tF8t1LApwfPQ85VKdCr2+Bc4bloFpzrE7TnbYR9LQJM6l6UGgqewLVcWwwLS+nD86DK9Kfis6PfqWQoLv7LOkz+Wfe0bx04pQ9dgaT5OnekcuQoSPOVOZ5E/JyBkqigW1KNpZnDbqgOnUlR1lixHRBt1hOW6qTBoKyW/hy++wDQo3WzBXEwxK+EBmNBUf78g3HuIP44q1i3iEmGGrRuZIKh/r2aRF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